package com.xcity.game.item.effect;

import java.text.MessageFormat;

import com.xcity.db.entity.Player;
import com.xcity.game.common.MessageConst;
import com.xcity.game.item.GameItem;
import com.xcity.game.item.ItemEffect;
import com.xcity.game.item.ItemException;
import com.xcity.game.item.listener.BagListener;
import com.xcity.game.worldevent.WorldEvent;
import com.xcity.game.worldevent.WorldEventService;

import naga.x.App;

public class RemoveWorldEventEffect implements ItemEffect {

	@Override
	public boolean effect(Player player, GameItem item, int count, BagListener listener) throws ItemException {
		int needCount = item.getTemplate().getParameter(0);
		if (count < needCount) {
			throw new ItemException(MessageFormat.format(MessageConst.ITEM_NOT_ENOUGH, item.getName()));
		}
		WorldEventService service = App.getApp().getServiceManager().get(WorldEventService.class);
		WorldEvent event = service != null ? service.getCurrentEvent() : null;
		if (event == null) {
			throw new ItemException(MessageConst.HAVE_NO_WORLD_EVENT);
		}
		player.getBuffs().removeBuff(WorldEventService.buffIdOffset + event.getId());
		return true;
	}

}
